#pragma once
#include "fstream.h"
#include "socket.h"
#include "task.h"
#include <cstdint>
#include <istream>
#include <unordered_map>

using namespace std::chrono_literals;

namespace cobt {
enum InfoHashStatus { IH_INIT, IH_GET_PEERS, IH_GET_METADATA, IH_DONE };
class DHTDigger;
class GetMetaFile {
public:
  GetMetaFile(const SocketAddress &addr, const std::string &info_hash,
              const std::string &id);
  Task<std::string> get_meta_file(const DHTDigger& digger);

  bool ext_handshake(std::istream &ss);

private:
  Task<bool> handshake();
  Task<std::string> recv_metainfo(const DHTDigger& digger);
  Task<bool> recv_msg(std::string &buf);
  Task<bool> send_msg(const std::string &msg, char msg_id);
  SocketAddress m_addr;
  std::string m_info_hash;
  FileStream m_fs;
  std::string m_id;
  int64_t m_metadata_size;
  int64_t m_ut_metadata;
};
} // namespace cobt
